from datetime import datetime
from app import db
from config import Config


class FileUpload(db.Model):
    """文件上传记录表"""
    __tablename__ = 'file_uploads'
    
    id = db.Column(db.Integer, primary_key=True)
    file_name = db.Column(db.String(255), nullable=False, comment='文件名')
    original_name = db.Column(db.String(255), comment='原始文件名')
    file_path = db.Column(db.String(500), nullable=False, comment='文件路径')
    file_size = db.Column(db.BigInteger, comment='文件大小(字节)')
    file_type = db.Column(db.String(50), comment='文件类型')
    mime_type = db.Column(db.String(100), comment='MIME类型')
    file_hash = db.Column(db.String(64), comment='文件哈希值')
    upload_user_id = db.Column(db.Integer, comment='上传用户ID')
    upload_user_type = db.Column(db.String(20), default='user', comment='上传用户类型：user普通用户,matchmaker红娘,admin管理员')
    usage_type = db.Column(db.String(50), comment='使用类型：avatar头像,photo照片,document文档')
    status = db.Column(db.Integer, default=1, comment='状态：1正常2已删除')
    created_at = db.Column(db.DateTime, default=datetime.utcnow, comment='创建时间')
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, comment='更新时间')
    
    # 使用upload_user_id作为uploaded_by的别名
    uploaded_by = db.synonym('upload_user_id')
    
    # 这些字段在数据库中不存在，但在代码中使用，所以定义为非映射属性
    __upload_type = None
    __related_id = None
    __is_public = None
    __file_url = None
    
    @property
    def upload_type(self):
        return self.__upload_type or 'general'
        
    @upload_type.setter
    def upload_type(self, value):
        self.__upload_type = value
        
    @property
    def related_id(self):
        return self.__related_id
        
    @related_id.setter
    def related_id(self, value):
        self.__related_id = value
        
    @property
    def is_public(self):
        return self.__is_public or False
        
    @is_public.setter
    def is_public(self, value):
        self.__is_public = value
        
    @property
    def file_url(self):
        return self.__file_url or f'/static/uploads/{self.file_name}'
        
    @file_url.setter
    def file_url(self, value):
        self.__file_url = value
    
    def to_dict(self):
        """转换为字典"""
        # 构建文件URL
        file_url = f"{Config.FILE_URL_PREFIX}/{self.file_name}"
        
        return {
            'id': self.id,
            'file_name': self.file_name,
            'original_name': self.original_name,
            'file_path': self.file_path,
            'file_size': self.file_size,
            'file_type': self.file_type,
            'mime_type': self.mime_type,
            'file_hash': self.file_hash,
            'uploaded_by': self.upload_user_id,
            'upload_type': self.upload_type,
            'is_public': self.is_public,
            'usage_type': self.usage_type,
            'file_url': file_url,
            'status': self.status,
            'status_text': self.get_status_text(),
            'created_at': self.created_at.strftime('%Y-%m-%d %H:%M:%S') if self.created_at else None,
            'updated_at': self.updated_at.strftime('%Y-%m-%d %H:%M:%S') if self.updated_at else None
        }
    
    def get_status_text(self):
        """获取状态文本"""
        status_map = {1: '正常', 2: '已删除'}
        return status_map.get(self.status, '未知')
    
    def __repr__(self):
        return f'<FileUpload {self.file_name}>' 